STACK_SEG SEGMENT STACK_SEG
        DW 200H DUP(0)
STACK_SEG ENDS

DATA SEGMENT
	ChoiceLen	DB 13;主菜单选项长度
	ChoiceRow	DB 13;主菜单选项行
	ChoiceCol	DB 30;主菜单选项列
	Choice1		DB ' Single Mode '
	
	Choice3		DB '    Quit     '
	GameMode	DB 0;游戏模式选择,0为单机,1为双机,2退出

	ChoiceSideLen	DB 48;选色菜单选项长度
	ChoiceSideRow	DB 13;选色菜单行起始位置
	ChoiceSideCol	DB 19;选色菜单列起始位置
	ChoiceMes	DB 'Chose side: press 0 the black, press 1 the white'
	ChoiceMes2	DB '       Press other keys to return menu          '
	ChoiceSide	DB 02;记录选择的色方

        WHITE DB 0FH;白
        BLACK DB 00H;黑
	
	MAP 		DB 200H DUP(33H);棋盘数组，存放各自的信息
	MapOffset	DW 0
	MapIndex 	DW 0	;到棋盘数组的索引
	MapRowDis 	DW 10	;棋盘行的上边距离屏幕长度
	MapColDis 	DW 165	;棋盘列的左边距离屏幕长度
	MapRowSize 	DW 310	;棋盘行的长度
	MapColSize 	DW 310	;棋盘列的长度
	MapColor 	DB 06H	;棋盘颜色,棕色
	RowMax 		DW 15	;最大行数
	ColMax 		DW 15	;最大列数

	X 	DW 0
	Y 	DW 0
	X2 	DW 0
	Y2 	DW 0
	I 	DW 0
	J 	DW 0

	CursorDis  	DW 5	;光标的距离
	LineRowDis 	DW 25	;线距离屏幕上方的距离
	LineColDis 	DW 180	;线距离屏幕左方的距离
	LineDis 	DW 20	;线的间隔
	LineHalfDis 	DW 10	;线的半间隔
	RSquare		DW 51	;半径平方和
	
	InfoLen		DB 11	;信息栏的信息长度
	InfoNameRow	DB 2	;名字栏的起始行
	InfoScoreRow	DB 4	;时间栏的起始行
        P2InfoCol       DB 62    ;白方信息栏起始列
        P1InfoCol       DB 5   ;黑方信息栏起始列
        P2ScoreCol      DB 73   ;白方时间栏起始列
        P1ScoreCol      DB 16   ;黑方信息栏起始列
	P2Score		DB 0
	P1Score		DB 0
	WhiteName	DB 'White Side:'
	BlackName	DB 'Black Side:'
	WhiteScore	DB 0
	BlackScore	DB 0
	P1Name		DB 'Player1:   '
	P2Name		DB 'Player2:   '
	ScoreInfo	DB 'Scores:    '
	
	CurColor	DB 0CH
	CurX 		DW 9	;光标当前横坐标
	CurY 		DW 9	
	LastCurX 	DW 0	;上步光标横坐标
	LastCurY 	DW 0
		
	Player 		DB 00H;
	PlayerSelf	DB 00H;代表自己
	

	XLOW	DW 0;棋子左侧
	XHIGH	DW 0
	YLOW	DW 0
	YHIGH	DW 0
	
	Win DW 0
	TotalStep	DB 0
        BottomInfoLen   DB 29
	BottomInfoRow	DB 22
	BottomInfoCol	DB 25
        BottomInfoMes   DB 'Press any key to return menu!'
        PickWhiteInfo   DB 'You are white side.          '
        PickBlackInfo   DB 'You are black side.          '
        BLACKWIN        DB 'Black side win!              '
        WHITEWIN        DB 'White side win!              '
        DrawInfo        DB 'Come to a draw!              '
	
DATA ENDS

;=====================保存寄存器,初始化寄存器=======================
PUSHALL MACRO
	PUSH AX
	PUSH BX
	PUSH CX
	PUSH DX
	MOV AX, 0
	MOV BX, 0
	MOV CX, 0
	MOV DX, 0
ENDM

;==========================恢复寄存===================================
POPALL MACRO
	POP DX
	POP CX
	POP BX
	POP AX
ENDM
;=================模式选择的显示部分=================================
ChoseModePage MACRO
	PUSHALL
	MOV AX, DS
	MOV ES, AX
	MOV AH, 13H
	MOV AL, 01H
	MOV BL, 0FH
	MOV CL,[ChoiceLen]
	MOV DH,[ChoiceRow]
	MOV DL,[ChoiceCol]

	MOV BP, OFFSET Choice1
	INT 10H
	INC DH
	
	MOV BP, OFFSET Choice3
	INT 10H
	POPALL
ENDM
;==============模式选择的控制部分=========================
ChoseMode MACRO 
	PUSHALL
	MOV [GameMode], 0;默认停留于第一个选项
	DrawArrow [WHITE]

CM0:	MOV AH, 01H
	INT 16H
	JZ CM0
	MOV AH, 07H
	INT 21H
	
	CMP AL, 32
	JNZ CM10
	JMP NEAR PTR CM9
CM10:	CMP AL, 0
	JNZ CM1
	MOV AH, 07H
	INT 21H
CM1:	CMP AL, 48H;上方向键
	JZ CM2
	JMP NEAR PTR CM5
CM2:	CMP [GameMode], 0
	JNZ CM3
	JMP NEAR PTR CM4
CM3:	DrawArrow [BLACK]
	DEC [GameMode]
	DrawArrow [WHITE]
CM4:	JMP NEAR PTR CM0

CM5:	CMP AL, 50H;下方向键
	JZ CM6
	JMP NEAR PTR CM8
CM6:	CMP [GameMode], 2
	JNZ  CM7
	JMP NEAR PTR CM8
CM7:	DrawArrow [BLACK]
	INC [GameMode]
	DrawArrow [WHITE]
CM8:	JMP NEAR PTR CM0

CM9:	POPALL
ENDM
;===================画箭头部分=============================
DrawArrow MACRO Color
	PUSHALL
	
	MOV AH, 2;置光标于选项左方,画左箭头
	MOV BX, 0
	MOV DH,[ChoiceRow]
	MOV DL,[GameMode]
	ADD DH, DL
	MOV DL,[ChoiceCol]
	INT 10H

	MOV AH, 0EH
	MOV AL, 3EH;左箭头
	MOV BL, Color
	MOV CX, 1
	INT 10H	

	MOV AH, 2;置光标于选项右方,画右箭头
	MOV BX, 0
	ADD DL, [ChoiceLen]
	DEC DL
	INT 10H
	
	MOV AH, 0EH
	MOV AL, 3CH;右箭头
	MOV BL, Color
	MOV CX, 1
	INT 10H

	POPALL
ENDM
;===================选色提示===============================
ChoseSidePage MACRO
PUSHALL
	MOV AX, DS
	MOV ES, AX
	MOV AH, 13H
	MOV AL, 01H
	MOV BL, 0FH
	MOV CL,[ChoiceSideLen]
	MOV DH,[ChoiceSideRow]
	MOV DL,[ChoiceSideCol]

	MOV BP, OFFSET ChoiceMes
	INT 10H
	INC DH
	MOV BP, OFFSET ChoiceMes2
	INT 10H
	POPALL
ENDM
;=====================发送选边信息===========================
SendSideInfo MACRO
	PUSHALL
SSI0:	MOV AL, 1
	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 02H
	JZ SSI5
	MOV DX, 0E4B8H
	IN AL, DX
	CMP AL, 1
	JZ SSI5
	MOV [ChoiceSide], AL
	JMP SSI3
SSI5:	MOV AH, 01H
	INT 16H
	JZ SSI0
	MOV AH, 07H
	INT 21H
	
	CMP AL, 0
	JNZ SSI1
	MOV AH, 07H
	INT 21H
SSI1:	CMP AL, 30H
	JNZ SSI2
	MOV AL, [BLACK]
	MOV [ChoiceSide], AL
SSI6:	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 01H
	JZ SSI6
	MOV DX, 0E4B8H
	MOV AL, [WHITE]
	OUT DX, AL
	;SendInfo AL
	JMP SSI3

SSI2:	CMP AL, 31H
	JNZ SSI4
	MOV AL, [WHITE]
	MOV [ChoiceSide], AL
SSI7:	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 01H
	JZ SSI7
	MOV DX, 0E4B8H
	MOV AL, [BLACK]
	OUT DX, AL
	;SendInfo AL
	JMP SSI3

SSI4:	MOV AL, 3
	MOV [ChoiceSide], AL
SSI3:	POPALL
ENDM
 
;====================初始化============================================
Init MACRO
	PUSHALL
	MOV AL, 1
	MOV AL, [BLACK];黑方先行
	MOV [Player], AL

	CMP [GameMode], 1
	JNZ IT2
	MOV AL, [ChoiceSide]
	MOV [PlayerSelf],AL
        JMP IT5

IT2:	MOV [PlayerSelf],AL
	
IT5:    MOV CX, 0
	MOV BX, OFFSET MAP
	MOV AX, 33H
IT0:	CMP CX, 1FFH
	JA IT1
	MOV [BX], AX
	INC CX
	INC BX
	JMP IT0
IT1:	MOV [CurX],8
	MOV [CurY],8
	MOV [LastCurX],0
	MOV [LastCurY],0
	MOV [Win], 0
	POPALL
ENDM
	
;===============通过行号,返回行的横坐标================
GETX MACRO I, X
	PUSHALL
	MOV AX,I
	DEC AX
	MOV BX, [LineDis]
	MUL BL
	ADD AX, [LineRowDis]
	MOV X, AX
	POPALL
ENDM
;===============通过列号,返回列的纵坐标=================	
GETY MACRO J, Y
	PUSHALL
	MOV AX, J
	DEC AX
	MOV BX, [LineDis]
	MUL BL
	ADD AX, [LineColDis]
	MOV Y, AX
	POPALL
ENDM	
;===============棋盘的显示部分==========================
DrawMap MACRO 
	PUSHALL

	MOV BX,[MapRowDis];画棋盘底色
	MOV DX, BX
	ADD BX,[MapRowSize]
	MOV [X], BX	
DM0:	CMP DX, [X]
	JAE DM3
	MOV BX, [MapColDis]
	MOV CX, BX
	ADD BX, [MapColSize]
	MOV [Y], BX
DM1:	CMP CX, [Y]
	JAE DM2
	MOV BX, 0
	MOV AH, 0CH
	MOV AL, [MapColor]
	INT 10H
	INC CX
	JMP DM1
DM2:	INC DX
	JMP DM0

DM3:	MOV AX, [RowMax]
	MOV BX, [ColMax]
	MOV [I], 1
DM4:	CMP [I], AX;画行
	JBE DM12
	JMP NEAR PTR DM7
DM12:	PUSH AX
	GETX [I],[X]
	MOV [J], 1
	GETY [J],[Y]
	MOV AX, [ColMax]
	MOV [J], AX
	GETY [J], [Y2]
	MOV BX, [Y2]
DM5:	CMP [Y], BX;画一行
	JA DM6
	PUSH BX
	MOV BX, 0
	MOV AL, [BLACK]
	MOV DX, [X]
	MOV CX, [Y]
	MOV AH, 0CH
	INT 10H
	INC [Y]
	POP BX
	JMP DM5
DM6:	INC[I]
	POP AX
	JMP DM4

DM7:	MOV AX, [RowMax]
	MOV BX, [ColMax]
	MOV [J], 1
DM8:	CMP [J], BX;画列
	JBE DM13
	JMP NEAR PTR DM11 
DM13:	PUSH BX
	GETY [J], [Y]
	MOV [I], 1
	GETX [I], [X]
	MOV BX, [RowMax]
	MOV [I], BX
	GETX [I], [X2]
	MOV AX, [X2]
DM9:	CMP [X], AX
	JA DM10
	PUSH AX
	MOV BX, 0
	MOV AL, [BLACK]
	MOV DX, [X]
	MOV CX, [Y]
	MOV AH, 0CH
	INT 10H
	INC [X]
	POP AX
	JMP DM9
DM10:	INC [J]
	POP BX
	JMP DM8
DM11:	MOV AX, DS;	显示左右信息栏信息
	MOV ES, AX
	MOV AH, 13H
	MOV AL, 01H
	MOV BH, 0
	MOV BL, 0FH
	MOV CX, 0
	MOV CL, [InfoLen]
	MOV DL, [P2InfoCol]
	MOV DH, [InfoNameRow]
	CMP [GameMode], 1
	JNZ DM17
	MOV BP, OFFSET P2Name
	JMP DM18 
DM17:	MOV BP, OFFSET WhiteName
DM18:	INT 10H
	MOV DH, [InfoScoreRow]
	MOV BP, OFFSET ScoreInfo
	INT 10H
	MOV DL, [P1InfoCol]
	INT 10H
	MOV DH, [InfoNameRow]
	CMP [GameMode], 1
	JNZ DM19
	MOV BP, OFFSET P1Name
	JMP DM20
DM19:	MOV BP, OFFSET BlackName
DM20:	INT 10H

	MOV AH, 2
	MOV BX, 0
	MOV DL, [P1InfoCol]
	ADD DL, 8
	MOV DH, [InfoScoreRow]
	INT 10H
	MOV AH, 0EH
	MOV BH, 0
	MOV BL, 0FH
	MOV DH, 0
	MOV CX, 1
	CMP [GameMode], 1
	JZ DM21
	MOV AL, [BlackScore]
	ADD AL, 30H
	INT 10H
	JMP DM22
DM21:	MOV AL, [P1Score]
	ADD AL, 30H
	INT 10H
		
DM22:	MOV AH, 2
	MOV BX, 0
	MOV DL, [P2InfoCol]
	ADD DL, 8
	MOV DH, [InfoScoreRow]
	INT 10H
	MOV AH, 0EH
	MOV BH, 0
	MOV BL, 0FH
	MOV DH, 0
	MOV CX, 1
	CMP [GameMode], 1
	JZ DM23
	MOV AL, [WhiteScore]
	ADD AL, 30H
	INT 10H
	JMP DM24
DM23:	MOV AL, [P2Score]
	ADD AL, 30H
	INT 10H
	
DM24:	MOV AL, 1
	CMP [GameMode], AL
	JNZ DM16
	MOV AX, DS;	色方信息
	MOV ES, AX
	MOV AH, 13H
	MOV AL, 1
	MOV BH, 0
	MOV BL, 0FH
	MOV CX, 0
	MOV CL, [BottomInfoLen]
	MOV DL, [BottomInfoCol]
	MOV DH, [BottomInfoRow]
	MOV AL, [BLACK]
	CMP [ChoiceSide], AL
	JNZ DM14
	MOV BP, OFFSET PickBlackInfo
	JMP DM15
DM14:	MOV BP, OFFSET PickWhiteInfo
DM15:	INT 10H	
DM16:	POPALL

ENDM

;=====================游戏开始========================
Game MACRO
	PUSHALL
GM0:	MOV AL, 225
	CMP AL, [TotalStep];无棋子可下，和棋
	JNZ GM50
	MOV AL, 1
	MOV [Player], AL
	CALL FAR PTR DISPWinInfo
	JMP NEAR PTR GM14

GM50:	CALL FAR PTR DISPCursor
GM16:	MOV AL, 1
	CMP [GameMode], AL
        JZ GM40
        JMP NEAR PTR GM2
GM40:   MOV AL, [Player]
	CMP [PlayerSelf], AL
        JNZ GM41
        JMP NEAR PTR GM2
GM41:   PUSH AX
        MOV AX, 06
        MOV [CurColor],AL
        CALL FAR PTR DISPCursor
        MOV AL, 0CH
        MOV [CurColor],AL
        POP AX
GM30:   MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 02H
        JNZ GM31
        MOV AH, 01
        INT 16H
        JZ GM30
        MOV AH, 07
        INT 21H
        CMP AL, 27
        JNZ GM30
        JMP NEAR PTR GM14
GM31:   MOV DX, 0E4B8H
	IN AL, DX
	MOV [CurX], AL
GM20:	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 02H
        JNZ GM32
        MOV AH, 01
        INT 16H
        JZ GM20
        MOV AH, 07
        INT 21H
        CMP AL, 27
        JNZ GM20
        JMP NEAR PTR GM14
GM32:   MOV DX, 0E4B8H
	IN AL, DX
	MOV [CurY], AL
        GetMap [CurX], [CurY]
        MOV BX, [MapIndex]
        MOV AL, [Player]
        MOV [BX], AL
        JMP NEAR PTR GM17

GM2:	MOV AH, 01H
	INT 16H
        JZ GM2
GM35:   MOV AH, 07H
	INT 21H

	CMP AL, 27	;esc退出
	JNZ GM3
	JMP NEAR PTR GM14

GM3:	CMP AL, 32
	JZ GM4
	JMP NEAR PTR GM8
GM4:	GetMap [CurX],[CurY]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, 33H	;是否有空位
	JZ GM5
	JMP NEAR PTR GM2
GM5:	MOV AL, [Player];在map数组上标记
	MOV [BX], AL
	MOV AX, [CurX];		记录上步光标
	MOV [LastCurX], AX
	MOV AX, [CurY]
	MOV [LastCurY], AX
	
	MOV AX, 1	;联机模式下需要发送落子信息，以数组偏移量表示
	CMP [GameMode], AL
	JNZ GM17
GM18:	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 01H
	JZ GM18
	MOV DX, 0E4B8H
	MOV AL, [CurX]
	OUT DX, AL
GM21:	MOV DX, 0E4B9H
	IN AL, DX
	TEST AL, 01H
	JZ GM21
	MOV AL, [CurY]
	MOV DX, 0E4B8H
	OUT DX, AL

GM17:	DrawPiece [CurX], [CurY], [Player];画棋子
	CheckVictory [CurX], [CurY], [Player]; 判胜
	INC [TotalStep]
	CMP [WIN], 1
	JZ GM6
	MOV AL, [Player]
	CMP AL, [Black];交换控制权
	JNZ GM15
	MOV AL, [White]
	MOV [Player], AL
	JMP NEAR PTR GM0
GM15:	MOV AL, [Black]
	MOV [Player], AL
	JMP NEAR PTR GM0
GM6:	MOV AL, 1
	CMP [GameMode], AL
	JNZ GM60
	MOV AL, [Player]
	CMP [ChoiceSide], AL
	JZ GM61
	INC [P2Score]
	JMP GM62
GM61:	INC [P1Score]
	JMP GM62
GM60:	MOV AL, [Player]
	CMP AL, [BLACK]
	JZ GM63
	INC [WhiteScore]
	JMP GM62
GM63:	INC [BlackScore]
GM62:	CALL FAR PTR DISPWinInfo
	JMP NEAR PTR GM14

GM8:	CMP AL, 0
	JNZ GM9
	MOV AH, 07H
	INT 21H
GM9:    PUSH AX			
	MOV AX, 06H
	MOV [CurColor],AL
	CALL FAR PTR DISPCursor
	MOV AL, 0CH
	MOV [CurColor],AL
	POP AX
	
	CMP AL, 48H;上
	JNZ GM10
	CMP [CurX], 1
	JBE GM10
	DEC [CurX]
GM10:	CMP AL, 50H;下
	JNZ GM11
	CMP [CurX], 15
	JAE GM11
	INC [CurX]
GM11: CMP AL, 4BH;左
	JNZ GM12
	CMP [CurY], 1
	JBE GM12
	DEC [CurY]
GM12:	CMP AL, 4DH;右
	JNZ GM13
	CMP [CurY], 15
	JAE GM13
	INC [CurY]
GM13:	CALL FAR PTR DISPCursor
        JMP NEAR PTR GM2
GM14:	POPALL
ENDM
;=====================通过棋盘坐标得到相应格子的坐标,存于MapIndex中=======
GetMap MACRO x, y
	PUSHALL
	MOV AX, x
	MOV BX, [ColMax]
	ADD BX, 1;将边界列考虑进去
	MUL BX
	ADD AX, y
	MOV [MapOffset], AX
	MOV BX, OFFSET MAP
	ADD BX, AX
	MOV [MapIndex], BX
	POPALL
ENDM

;==============判断落子后是否取得胜利====================
CheckVictory MACRO XX,YY,Color
	PUSHALL
	
	MOV AX,XX
	MOV [X],AX
	MOV AX,YY
	MOV [Y],AX
	MOV CX,0
	
CV0:	CMP CX, 6
	JAE CV1
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV1
	INC [X]
	INC CX
	JMP CV0
CV1:	MOV AX,XX
	MOV [X],AX
	MOV AX,YY
	MOV [Y],AX
CV2:	CMP CX, 6
	JAE CV19
	GetMap [X],[Y],[MapIndex]
	MOV BX, [MapIndex]
	MOV AL,[BX]
	CMP AL, Color
	JNZ CV19
	DEC [X]
	INC CX
	JMP CV2
CV19:	CMP CX, 6
	JB CV3
	MOV [Win], 1


CV3:	MOV AX,XX
	MOV [X],AX
	MOV AX,YY
	MOV [Y],AX
	MOV CX,0
CV4:	CMP CX, 6
	JAE CV5
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV5
	INC [Y]
	INC CX
	JMP CV4
CV5:	MOV AX, XX
	MOV [X], AX
	MOV AX, YY
	MOV [Y], AX
CV6:	CMP CX, 6
	JAE CV7
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV7
	DEC [Y]
	INC CX
	JMP CV6
CV7:	CMP CX, 6
	JB CV8
	MOV [Win], 1

CV8:	MOV AX, XX
	MOV [X], AX
	MOV AX, YY
	MOV [Y], AX
	MOV CX, 0
CV9:	CMP CX, 6
	JAE CV10
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV10
	INC [X]
	INC [Y]
	INC CX
	JMP CV9
CV10:	MOV AX, XX
	MOV [X], AX
	MOV AX, YY
	MOV [Y], AX
CV11:	CMP CX, 6
	JAE CV12
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV12
	DEC [X]
	DEC [Y]
	INC CX
	JMP CV11
CV12:	CMP CX, 6
	JB CV13
	MOV [Win], 1

CV13:	MOV AX, XX
	MOV [X], AX
	MOV AX, YY
	MOV [Y], AX
	MOV CX, 0
CV14:	CMP CX, 6
	JAE CV15
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV15
	INC [X]
	DEC [Y]
	INC CX
	JMP CV14
CV15:	MOV AX, XX
	MOV [X], AX
	MOV AX, YY
	MOV [Y], AX
CV16:	CMP CX, 6
	JAE CV17
	GetMap [X], [Y], [MapIndex]
	MOV BX, [MapIndex]
	MOV AL, [BX]
	CMP AL, Color
	JNZ CV17
	DEC [X]
	INC [Y]
	INC CX
	JMP CV16
CV17:	CMP CX, 6
	JB CV18
	MOV [Win], 1
CV18:	POPALL
ENDM

;=====================画棋子====================
DrawPiece MACRO i, j, PieceColor
	PUSHALL
	GETX i, [X]
	GETY j, [Y]
	MOV BX, [LineHalfDis]
	DEC BX
	
	MOV AX, [X]
	MOV [X2], AX
	MOV [XLOW], AX
	SUB [XLOW], BX
	MOV [XHIGH], AX
	ADD [XHIGH], BX

	MOV AX, [Y]
	MOV [Y2], AX
	MOV [YLOW], AX
	SUB [YLOW], BX
	MOV [YHIGH], AX
	ADD [YHIGH], BX
	
	MOV BX, [XLOW]
	MOV [X], BX
	MOV BX, [XHIGH]
DRAWP1:	CMP [X], BX
	JA DRAWP2
	PUSH BX
	MOV BX, [YLOW]
	MOV [Y], BX
	MOV BX, [YHIGH]
DRAWP3:	CMP [Y], BX
	JA DRAWP7
	PUSH BX
	MOV AX, [X2]
	SUB AX, [X]
	CMP AX, 10H
	JBE DRAWP4
	MOV AX, [X]
	SUB AX, [X2]
DRAWP4:	MUL AX;平方
	MOV BX, AX

	MOV AX, [Y2]
	SUB AX, [Y]
	CMP AX, 10H
	JBE DRAWP5
	MOV AX, [Y]
	SUB AX, [Y2]
DRAWP5:	MUL AX
	ADD BX, AX

	CMP BX, [RSquare];范围内,改像素为棋子颜色
	JA DRAWP6
	MOV AH, 0CH
	MOV AL, PieceColor
	MOV BX, 0
	MOV DX, [X]
	MOV CX, [Y]
	INT 10H
DRAWP6:	INC [Y]
	POP BX
	JMP DRAWP3
DRAWP7: INC [X]
	POP BX
	JMP NEAR PTR DRAWP1
DRAWP2: POPALL
ENDM


;=============初始化8251A,8253=====================
InitChip MACRO
	PUSHALL

	MOV DX,0E483H;初始化8253
	MOV AL,00010100B
	OUT DX,AL
	MOV DX,0E480H
	MOV AL,52
	OUT DX,AL
	
	MOV DX,0E4B9H;初始化8251
	MOV AL,0
	OUT DX,AL
	OUT DX,AL
	OUT DX,AL
	MOV AL,40H
	OUT DX,AL
	MOV AL,4EH
	OUT DX,AL
	MOV AL,27H
	OUT DX,AL
	
	POPALL
ENDM
CODE SEGMENT
	ASSUME CS:CODE, SS:STACK_SEG, DS:DATA
START:	MOV AX, DATA
	MOV DS, AX
	MOV AX, STACK_SEG
	MOV SS, AX

	MOV AH, 0;设置显示方式
	MOV AL, 12H; 640*480 16色图形
	INT 10H
	InitChip
LOOP1: 	CMP [GameMode], 2
	JNZ LL0
	JMP NEAR PTR LLOUT

LL0:	ChoseModePage
	ChoseMode
	MOV AL, 2
	MOV [ChoiceSide], AL
	
	CMP [GameMode], 1;联机模式
	JZ LL5
	JMP NEAR PTR LL1
LL5:	CALL FAR PTR ClearScreen
	ChoseSidePage; 挑边，先挑先得
	SendSideInfo;  发送信息
	CALL FAR PTR ClearScreen
	MOV AL, 3
	CMP [ChoiceSide], AL
	JNZ LL2
	JMP NEAR PTR LOOP1

LL1:	CMP [GameMode], 2;退出
	JNZ LL2
	JMP NEAR PTR LLOUT

LL2:	Init	;初始化
	DrawMap	;画棋盘
	Game	;开始游戏
LL3:	MOV AH, 1
	INT 16H
	JZ LL3
	MOV AH, 07
	INT 21H 
	CALL FAR PTR ClearScreen 
	JMP NEAR PTR LOOP1
LLOUT:	MOV AH, 1
	INT 16H
	JZ LLOUT
	CALL FAR PTR ClearScreen
	MOV AH, 4CH
	INT 21H

;=====================在棋盘相应坐标画光标=========
DISPCursor PROC FAR
	GETX [CurX], [X]
	GETY [CurY], [Y]

	MOV AX, [X]
	SUB AX, [LineHalfDis]
	MOV [X2], AX
	MOV AX, [Y]
	SUB AX, [LineHalfDis]
	MOV [Y2], AX

	MOV AX, [X2]
	ADD AX, [CursorDis]
PDC0:	CMP [X2], AX
	JZ PDC1
	PUSH AX
	MOV AH, 0CH
	MOV AL, [CurColor]
	MOV BX, 0
	MOV DX, [X2]
	MOV CX, [Y2]
	INT 10H
	ADD CX, [LineDis]
	INT 10H
	POP AX
	INC [X2]
	JMP PDC0

PDC1:	SUB AX, [CursorDis]
	ADD AX, [LineDis]
	MOV [X2], AX
	SUB AX, [CursorDis]
PDC2:	CMP AX, [X2]
	JZ PDC3
	PUSH AX
	MOV AH, 0CH
	MOV AL, [CurColor]
	MOV BX, 0
	MOV DX, [X2]
	MOV CX, [Y2]
	INT 10H
	ADD CX, [LineDis]
	INT 10H
	POP AX
	DEC [X2]
	JMP PDC2

PDC3:	ADD AX, [CursorDis]
	MOV [X2], AX
	MOV AX, [Y2]
	ADD AX, [CursorDis]
PDC4:	CMP [Y2], AX
	JZ PDC5
	PUSH AX
	MOV AH, 0CH
	MOV AL, [CurColor]
	MOV BX, 0
	MOV DX, [X2]
	MOV CX, [Y2]
	INT 10H
	SUB DX, [LineDis]
	INT 10H
	POP AX
	INC [Y2]
	JMP PDC4

PDC5:	SUB AX, [CursorDis]
	ADD AX, [LineDis]
	MOV [Y2], AX
	SUB AX, [CursorDis]	
PDC6:	CMP AX, [Y2]
	JA PDC7
	PUSH AX
	MOV AH, 0CH
	MOV AL, [CurColor]
	MOV BX,	0
	MOV DX, [X2]
	MOV CX, [Y2]
	INT 10H
	SUB DX, [LineDis]
	INT 10H
	POP AX
	DEC [Y2]
	JMP PDC6
PDC7:	RET
;====================获胜信息===================
;=====================获胜信息=================
DISPWinInfo PROC FAR	
	PUSHALL
	MOV AX, DS
	MOV ES, AX
	MOV AL, Player
	CMP AL, [Black]
	JZ DWI0
	CMP AL, [White]
	JZ DWI2
	MOV BP, OFFSET DrawInfo
	JMP DWI1
DWI2:	MOV BP, OFFSET WHITEWIN
	JMP DWI1
DWI0:	MOV BP, OFFSET BLACKWIN
DWI1:	MOV AH, 13H
	MOV AL, 01H
	MOV BL, 0FH
	MOV CL, [BottomInfoLen]
	MOV DH, [BottomInfoRow]
	MOV DL, [BottomInfoCol]
	INT 10H
        INC DH
        MOV BP, OFFSET BottomInfoMes
        INT 10H
	POPALL
	RET
;=====================清除屏幕==================
ClearScreen PROC FAR
	PUSHALL
	MOV AH, 2;光标移到初始
	INT 10H
	MOV CX, 2500;持续输出空格清屏
	MOV AH, 02H
	MOV DL, 32
CC:	INT 21H
	LOOP CC
	MOV AH, 2;光标移到初始
	MOV DL, 0
	INT 10H
	POPALL
	RET

CODE ENDS
END START
